package 行为型模式.观察者;


import java.util.ArrayList;
import java.util.List;

/**
 * @ClassNameObserveTest
 * @Description 抽象目标 ->目标 （删改）观察者
 * @Author
 * @Date2022/1/5 16:47
 **/
public class ObserveTest {
    public static void main(String[] args) {

        Rate rmBrate = new RMBrate();
        Company importCompany = new ImportCompany();
        Company exportCompany = new ExportCompany();
        rmBrate.add(importCompany);
        rmBrate.add(exportCompany);

        rmBrate.change(2);
        System.out.println("=================");
        rmBrate.add(exportCompany);
        rmBrate.change(-3);
        System.out.println("=================");
        rmBrate.remove(exportCompany);
        rmBrate.change(5);
    }
}

// 抽象目标
abstract class Rate{
     List<Company> companies = new ArrayList<>();

    void add(Company company){
        companies.add(company);
    }
    void remove(Company company){
        companies.remove(company);
    }

    public abstract void change(int number);
}

//具体目标：人民币汇率
class RMBrate extends Rate {
    @Override
    public void change(int number) {
        for (Company obs : companies) {
            ((Company) obs).response(number);
        }
    }
}

// 抽象观察者
interface Company{
    void response(int number);
}
//具体观察者1：进口公司
class ImportCompany implements Company {
    @Override
    public void response(int number) {
        if (number > 0) {
            System.out.println("人民币汇率升值" + number + "个基点，降低了进口产品成本，提升了进口公司利润率。");
        } else if (number < 0) {
            System.out.println("人民币汇率贬值" + (-number) + "个基点，提升了进口产品成本，降低了进口公司利润率。");
        }
    }
}
//具体观察者2：出口公司
class ExportCompany implements Company {
    @Override
    public void response(int number) {
        if (number > 0) {
            System.out.println("人民币汇率升值" + number + "个基点，降低了出口产品收入，降低了出口公司的销售利润率。");
        } else if (number < 0) {
            System.out.println("人民币汇率贬值" + (-number) + "个基点，提升了出口产品收入，提升了出口公司的销售利润率。");
        }
    }
}
